//
//  VideoViewController.m
//  Taste
//
//  Created by 徐昊 on 16/9/12.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "VideoViewController.h"
#import "FatCollectionViewCell.h"
#import "ZWYNetTool.h"
#import "VideoModel.h"
#import "VideoRes.h"
#import "HXMacro.h"
#import "ShowDetailViewController.h"
#import "PersonViewController.h"
#import "HomePageDetailViewController.h"
#import "MasterCookBookViewController.h"

#define kHomePageCollectionViewFatCellName @"HomePageCollectionViewFatCellNameReuse"
@interface VideoViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) VideoModel *model;

@end

@implementation VideoViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"视频";
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"LZUniversal_BarButtonItem_Pop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem = back;
    _layout.minimumInteritemSpacing = 5;
    _layout.minimumLineSpacing = 5;
    _layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerNib:[UINib nibWithNibName:@"FatCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kHomePageCollectionViewFatCellName];
    NSDictionary *dic = @{@"version" : @"2.50",
                          @"Content-Type" : @"application/x-www-form-urlencoded",
                          @"Content-Length" : @"15"};
    [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=index&a=getlistVideo" andBody:@"keyword=&page=1" andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _model = [VideoModel modelWithDic:result];
        [_collectionView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败");
        
    }];
}
- (void)back:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        _layout.itemSize = CGSizeMake(SCREEN_WIDTH - 10, (SCREEN_WIDTH - 10) / 1.78  + 40);
        return _layout.itemSize;
    }
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 15) / 2, (SCREEN_WIDTH - 15) / 2 / 1.78  + 30);
    return _layout.itemSize;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _model.res.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FatCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHomePageCollectionViewFatCellName forIndexPath:indexPath];
    cell.res = _model.res[indexPath.row];
    cell.label.numberOfLines = 2;
    cell.label.font = [UIFont systemFontOfSize:15];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([_model.res[indexPath.row].cid isEqualToString:@"3"]) {
        ShowDetailViewController *vc = [[ShowDetailViewController alloc] init];
        vc.idString = _model.res[indexPath.row].sid;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if ([_model.res[indexPath.row].cid isEqualToString:@"16"]) {
        PersonViewController *vc = [[PersonViewController alloc] init];
        vc.numString = _model.res[indexPath.row].sid;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if ([_model.res[indexPath.row].cid isEqualToString:@"1"]) {
        HomePageDetailViewController *vc = [[HomePageDetailViewController alloc] init];
        vc.numString = _model.res[indexPath.row].sid;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if ([_model.res[indexPath.row].cid isEqualToString:@"14"]) {
        MasterCookBookViewController *vc = [[MasterCookBookViewController alloc] init];
        vc.numString = _model.res[indexPath.row].sid;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
